package com.example.appmarket.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.appmarket.R;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends AppCompatActivity {
    EditText etUsername,etPassword;
    TextView tv_reg;
    CheckBox cb_remember;
    Button btn_login;
    SharedPreferences userInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        userInfo = getSharedPreferences("userInfo",MODE_PRIVATE);
        initView();
        initEvents();
    }

    private void initEvents() {
        tv_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(),RegisterActivity.class));
            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = etUsername.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                BmobUser bmobUser = new BmobUser();
                bmobUser.setUsername(username);
                bmobUser.setPassword(password);
                bmobUser.login(new SaveListener<BmobUser>() {
                    @Override
                    public void done(BmobUser bmobUser, BmobException e) {
                        if(e==null){
                            if (cb_remember.isChecked()){
                                SharedPreferences.Editor editor = userInfo.edit();
                                editor.putBoolean("isRem",true);
                                editor.putString("username",username);
                                editor.putString("password",password);
                                editor.commit();
                                editor.clear();
                            }
                            else {
                                SharedPreferences.Editor editor = userInfo.edit();
                                editor.putBoolean("isRem",false);
                                editor.putString("username",null);
                                editor.putString("password",null);
                                editor.commit();
                                editor.clear();
                            }
                            startActivity(new Intent(getApplicationContext(),MainActivity.class).putExtra("username",username));
                            finish();
                        }else {
                            Toast.makeText(LoginActivity.this, "用户名或者密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }


    private void initView() {
        etUsername=findViewById(R.id.username);
        etPassword=findViewById(R.id.password);
        tv_reg=findViewById(R.id.tv_reg);
        cb_remember=findViewById(R.id.cb_remember);
        btn_login=findViewById(R.id.btn_login);

        if(userInfo.getBoolean("isRem",false)==true){
            etUsername.setText(userInfo.getString("username",""));
            etPassword.setText(userInfo.getString("password",""));
            cb_remember.setChecked(true);
        }
    }
}